﻿/*
 * Created by SharpDevelop.
 * User: 65949
 * Date: 5/12/2011
 * Time: 8:12 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Drawing;
using System.Windows.Forms;

namespace Stetson
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		Keyboard keyboard;
		public static Stetson stetson;
		MemoryView memoryView;
		
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			stetson = new Stetson();
			stetson.MdiParent = this;
			stetson.StartPosition = FormStartPosition.Manual;
			stetson.Location = new Point(300, 1);
			stetson.Show();
			
			memoryView = new MemoryView();
			memoryView.MdiParent = this;
			memoryView.StartPosition = FormStartPosition.Manual;
			memoryView.Location = new Point(300, 225);
			memoryView.Show();
			
			keyboard = new Keyboard();
			keyboard.MdiParent = this;
            //keyboard.EmulatorKeyDown += delegate(object sender, EmulatorKeyEventArgs e)
            //{
            //    stetson.EmulatorKeyDown(e.Keyname);
            //};
            //keyboard.EmulatorKeyUp += delegate(object sender, EmulatorKeyEventArgs e)
            //{
            //    stetson.EmulatorKeyUp(e.Keyname);
            //};
			keyboard.StartPosition = FormStartPosition.Manual;
			keyboard.Location = new Point(0, 0);
			keyboard.Show();
			
			Application.Idle += delegate
			{
				memoryView.Invalidate();
			};
			
			this.KeyPreview = true;
			this.KeyDown += mainForm_KeyDown;
		}

		void mainForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.F10)
			{
				e.SuppressKeyPress = true;
				ToggleEmulation();
			}
			if (e.KeyCode == Keys.F11)
			{
				e.SuppressKeyPress = true;
				StepEmulation();
			}
		}
		
		void PauseEmulationToolStripMenuItemClick(object sender, EventArgs e)
		{
			ToggleEmulation();
		}
		
		void ToggleEmulation()
		{
			if (stetson.IsRunning)
			{
				pauseEmulationToolStripMenuItem.Text = "Resume Emulation";
			}
			else
			{
				pauseEmulationToolStripMenuItem.Text = "Pause Emulation";
			}
			stetson.IsRunning = !stetson.IsRunning;
		}
		
		void StepEmulation()
		{
            MainForm.stetson.calculator.FetchExecute(-1);
		}
		
		void StepToolStripMenuItemClick(object sender, EventArgs e)
		{
			MessageBox.Show(memoryView.DesktopBounds.ToString());
		}
	}
}
